﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Diagnostics;
using System.IO;

namespace MNCServer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // create server
                Server server;

                // temp variables for TryParse()
                int temp = 0; 
                short tempS = 0;

                if (tabControl.SelectedIndex == 0) // create crossfire server
                    server = new CrossfireServer()
                    {
                        AllowBotPurchase = (bool)chkAllowBotPurchase.IsChecked,
                        AllowJuicePurchase = (bool)chkJuice.IsChecked,
                        AllowHazards = (bool)chkAllowHazards.IsChecked,
                        AllowMascot = (bool)chkAllowMascot.IsChecked,
                        AllowCustomClasses = (bool)chkAllowCustomClass.IsChecked,
                        AllowChangeClass = (bool)chkAllowChangeClass.IsChecked,
                        ForceRandomClass = (bool)chkForceRandomClass.IsChecked,
                        IsLanMatch = (bool)chkIsLanMatch.IsChecked,
                        AllowTeamSelect = (bool)chkAllowTeamSelect.IsChecked,
                        AutoBalance = (bool)chkAutoBalance.IsChecked,
                        MatchDuration = int.TryParse(txtMatchDuration.Text, out temp) ? temp : 2,
                        OvertimeDuration = int.TryParse(txtOvertimeDuration.Text, out temp) ? temp : 2,
                        ClassLimitAssault = short.TryParse(txtAssaultLimit.Text, out tempS) ? tempS : (short)6,
                        ClassLimitTank = short.TryParse(txtTankLimit.Text, out tempS) ? tempS : (short)6,
                        ClassLimitSupport = short.TryParse(txtSupportLimit.Text, out tempS) ? tempS : (short)6,
                        ClassLimitAssassin = short.TryParse(txtAssassinLimit.Text, out tempS) ? tempS : (short)6,
                        ClassLimitGunner = short.TryParse(txtGunnerLimit.Text, out tempS) ? tempS : (short)6,
                        ClassLimitSniper = short.TryParse(txtSniperLimit.Text, out tempS) ? tempS : (short)6,
                        MinPlayersToStart = short.TryParse(txtMinPlayers.Text, out tempS) ? tempS : (short)2,
                        MaxStarRating = 4,
                    };
                else if (tabControl.SelectedIndex == 1) // create blitz server
                    server = new BlitzServer()
                    { Challenge = (BlitzGameType)cboGameType.SelectedIndex };
                else return; // error in server selection

                // general server variables
                server.Name = txtName.Text;
                server.IsPassworded = (bool)chkIsPassworded.IsChecked;
                if (server.IsPassworded)
                    server.Password = txtPassword.Password;

                // launch
                Process pcs = new Process();
                pcs.StartInfo = new ProcessStartInfo(txtPath.Text, server.ArgumentString);
                pcs.Start();

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    }
}
